package com.example.demonettyclient;

import com.heyi.nettylong.client.listener.ChannelDataListener;
import com.heyi.nettylong.common.MessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author dengfeng
 * @date 2022/4/217:59
 */
@Slf4j
@Component
public class NettyClientListener implements ChannelDataListener {

    public NettyClientListener(){
        log.info("-------------NettyClientListener start........");
    }
    @Override
    public void authority(boolean auth) {
        if(auth) {
            log.info("连接服务端验证通过");
        }else {
            log.info("连接服务端验证没通过");
        }
    }

    @Override
    public void receiveData(MessageHandler messageHandler) throws InterruptedException {
        log.info("收到数据:{}--{}",messageHandler.getMessage().getMsgId(), messageHandler.getMessage().getData());
        if(messageHandler.getMessage().getMsgId() > 0) {
            // 自动回复
            messageHandler.sendMessage("回复:" + messageHandler.getMessage().getData());
        }
    }

    @Override
    public void disconnected() {
        log.info("连接已断开");
    }
}
